package org.curiouscreature.android.shelves.util;

import org.curiouscreature.android.shelves.R;
import org.curiouscreature.android.shelves.activity.AddBookActivity;
import org.curiouscreature.android.shelves.provider.BookStoreFactory;
import org.curiouscreature.android.shelves.provider.BooksManager;
import org.curiouscreature.android.shelves.provider.BooksStore;
import org.curiouscreature.android.shelves.provider.BooksStore.Book;
import org.curiouscreature.android.shelves.scan.ScanIntent;


import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewStub;
import android.widget.ProgressBar;
import android.widget.TextView;

public class PublicUtilites {

    public static final int REQUEST_SCAN_FOR_ADD = 1;
    public static final int REQUEST_SCAN_FOR_CHECK = 2;
    public static Activity activity;
    public AddTask mAddTask;
    public static void onAddSearch() {
        AddBookActivity.show(activity);
    }

    public static void onAdd() {
        startScan(REQUEST_SCAN_FOR_ADD);
    }

    public static void onCheck() {
        startScan(REQUEST_SCAN_FOR_CHECK);
    }
    
    public static void startScan(int code) {
        try {
            final Intent intent = new Intent(ScanIntent.INTENT_ACTION_SCAN);
            intent.putExtra(ScanIntent.INTENT_EXTRA_SCAN_MODE,
                    ScanIntent.INTENT_EXTRA_PRODUCT_MODE);
            activity.startActivityForResult(intent, code);
        } catch (ActivityNotFoundException e) {
            UIUtilities.showToast(activity, R.string.error_missing_barcode_scanner, true);
        }
    }
    
	public static void init(Activity activity){
		PublicUtilites.activity = activity;
	}
	
    public boolean onCreateOptionsMenu(Menu menu) {
        activity.getMenuInflater().inflate(R.menu.shelves, menu);
        return activity.onCreateOptionsMenu(menu);
    }
    
    public boolean onPrepareOptionsMenu(Menu menu) {
        final boolean scanAvailable = ScanIntent.isInstalled(activity);

        MenuItem item;
        item = menu.findItem(R.id.menu_item_add);
        item.setEnabled(scanAvailable && (mAddTask == null ||
                mAddTask.getStatus() == UserTask.Status.FINISHED));

        item = menu.findItem(R.id.menu_item_check);
        item.setEnabled(scanAvailable);

        return activity.onPrepareOptionsMenu(menu);
    }
    
    public class AddTask extends UserTask<String, Void, BooksStore.Book> {
        private final Object mLock = new Object();
        private String mBookId;

        @Override
        public void onPreExecute() {
 
        }

        String getBookId() {
            synchronized (mLock) {
                return mBookId;
            }
        }

 

        public BooksStore.Book doInBackground(String... params) {
            synchronized (mLock) {
                mBookId = params[0];
            }
           // return BooksManager.loadAndAddBook(getContentResolver(), mBookId,
           //         BookStoreFactory.get(activity));
           return null; 
                    
        }
 
    }
 
 
}
